// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// T [author] rhyme_lph
// t [email]  rhymelph@gmail.com
// S [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/entities/ocr_attachment_entity.dart';
import 'package:finance_app/src/utils/date_time_utils.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/attach_file_form.dart';
import 'package:finance_app/src/widgets/form/form_edit_status_inherited_widget.dart';
import 'package:finance_app/src/widgets/form/text_area_input_form.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/all_reports/fixed_asset/disposal_of_fixed_asset_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:tuple/tuple.dart';

@RouterMeta(paramName: "disposalOfFixedAssetPage")
class DisposalOfFixedAssetPage extends StatefulWidget {
  final String assetName;
  final int id;
  final String disposalDate;
  final String notes;
  final List<OcrAttachmentEntity> attachments;
  final bool isViewOnly;

  const DisposalOfFixedAssetPage(
      {Key key,
      this.assetName,
      this.disposalDate,
      this.notes,
      this.attachments,
      this.isViewOnly,
      this.id})
      : super(key: key);

  @override
  _DisposalOfFixedAssetPageState createState() =>
      _DisposalOfFixedAssetPageState();
}

class _DisposalOfFixedAssetPageState extends State<DisposalOfFixedAssetPage>
    with PagePresenter<DisposalOfFixedAssetPage, DisposalOfFixedAssetProvider> {
  @override
  void initState() {
    if (widget.isViewOnly != null && widget.isViewOnly) {
      provider.isEdit = false;
      provider.attachedFiles = widget.attachments;
      provider.notesController.text = widget.notes;
    }
    provider.id = widget.id;
    if (widget.disposalDate != null)
      provider.disposalDate = DateTime.tryParse(widget.disposalDate);
    super.initState();
  }

  Widget _buildSaveBtn() {
    return listBuilder<bool>(
      selector: (_, __) => pageStatus != PageStatus.loading && __.isEdit,
      builder: (BuildContext context, bool value, Widget child) => value == true
          ? NormalSaveTextButton(
              onTap: provider.onSave,
            )
          : Container(),
    );
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
      title: S.current.disposal_of_fixed_asset, actions: [_buildSaveBtn()]);

  @override
  Widget wrapperBody(Widget child) {
    return listBuilder<bool>(
      selector: (_, __) => __.isEdit == true,
      builder: (BuildContext context, bool value, Widget child) =>
          FormEditStatusInheritedWidget(
        isEdit: value,
        child: child,
      ),
      child: child,
    );
  }

  @override
  Widget get buildWidget => ListView(
        children: [
          ValueSelectForm(
            title: S.current.asset_name,
            value: widget.assetName,
          ),
          listBuilder<String>(
            selector: (_, __) =>
                DateTimeUtils.formatGlobalDateTime(__.disposalDate),
            builder: (BuildContext context, String value, Widget child) =>
                ValueSelectForm(
              title: S.current.disposal_date,
              value: value,
              onTap: provider.onChangeDisposalDate,
            ),
          ),
          AppSpaces.grayH6,
          TextAreaInputForm(
            title: S.current.memo_optional,
            controller: provider.notesController,
            focusNode: provider.notesFocusNode,
          ),
          NormalFormWrapper(
            child: listBuilder<Tuple2<List<String>, List<OcrAttachmentEntity>>>(
              selector: (_, __) =>
                  Tuple2(List.from(__.files), List.from(__.attachments ?? [])),
              builder: (BuildContext context,
                      Tuple2<List<String>, List<OcrAttachmentEntity>> value,
                      Widget child) =>
                  AttachFileForm(
                onAdd: provider.onAddAttachment,
                files: value.item1,
                onSelectFile: provider.onSelectFile,
                attachments: value.item2,
                onSelectAttachment: provider.onSelectAttachment,
              ),
            ),
          ),
        ],
      );
}
